/*=============================================================================

  SYSTEM MANAGER FRAME GRABBER INTERFACE MODULE

  FILE: SYSmgr_Fg.cpp
  
  DESCRIPTION

    Frame Grabber Interface Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <SYSmgr.h>

// FgDev Device Package
#include <HFg_cfg.h>
#include <devSP/fgHal_emul.h>
#include <devSP/fgHal_matsol.h>
#include "mainView.h"

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
static dword HFgDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser);

/*=============================================================================
  CLASS IMPLEMENTATION
=============================================================================*/
void SYSmgr::HFg_Alloc(void)
{
	ObjClient_s		Cli;

	m_pFg = new HFgDev;
	m_fgInit = FALSE;
	m_fgCFG = NULL;

	Cli.pUserCxt = this;
	Cli.EventCbf = HFgDevEventCallback;
	m_pFg->SetClient(&Cli);	

	// Register device support package
#ifdef TG_HW_FGDEV_SIMUL
	m_pFg->HAL_Register(FGDEV_EMULATOR, FgHal_Emul_Register, FgHal_Emul_Deregister);
#else
	m_pFg->HAL_Register(FGDEV_MATROX_SOLIOS, FgHal_MatSOL_Register, FgHal_MatSOL_Deregister);
#endif
}

void SYSmgr::HFg_Free(void)
{
	if(m_pFg == NULL)
		return;

	m_pFg->Close();

	if(m_fgCFG)
	{
		memFree(m_fgCFG);
		m_fgCFG = NULL;
	}
	
	// Deregister device support package
#ifdef TG_HW_FGDEV_SIMUL
	m_pFg->HAL_Deregister(FGDEV_EMULATOR);
#else
	m_pFg->HAL_Deregister(FGDEV_MATROX_SOLIOS);
#endif
	
	delete m_pFg;
	m_pFg = NULL;
}

boolean SYSmgr::HFg_Init(void)
{
	fgdev_cfg_s		*cfg;
	
	Event_Handler(HFG_DEV, INIT_START_E, HDEV_MAX, 0);

	if(m_fgInit == TRUE)
	{
		Event_Handler(HFG_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	// Check the configuration file
	cfg = fgdev_cfg_load(m_fgCFG);
	if(cfg == NULL)
	{
		LOGWARN("[FgDev] <%s> loading failure.", m_fgCFG);
		LOGWARN("[FgDev] Load the default configuration.");

		cfg = fgdev_default_alloc();
		if(fgdev_cfg_save(cfg, m_fgCFG) == FALSE)
		{
			fgdev_default_free(cfg);
			LOGERR("[FgDev] Default configuration save failure.");

			return	FALSE;
		}
		fgdev_default_free(cfg);
	}
	else
	{
		fgdev_cfg_free(cfg);
	}
	
	if(m_pFg->Init(m_fgCFG) == TRUE)
	{
		m_fgInit = TRUE;
		Event_Handler(HFG_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	Event_Handler(HFG_DEV, ERR_INIT_E, HDEV_MAX, 0);

	return	FALSE;
}

boolean SYSmgr::HFg_Close(void)
{
	Event_Handler(HFG_DEV, CLOSE_START_E, HDEV_MAX, 0);

	if(m_fgInit == FALSE)
	{
		Event_Handler(HFG_DEV, CLOSE_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	if(m_pFg->Close() == TRUE)
	{
		m_fgInit = FALSE;
		Event_Handler(HFG_DEV, CLOSE_DONE_E, HDEV_MAX, 0);

		return	TRUE;
	}

	m_fgInit = FALSE;
	Event_Handler(HFG_DEV, ERR_CLOSE_E, HDEV_MAX, 0);
	

	return	TRUE;
}

boolean SYSmgr::HFg_Setup(char *pszFile)
{
	if(m_fgCFG)
	{
		memFree(m_fgCFG);
		m_fgCFG = NULL;
	}

	if(pszFile != NULL)
	{
		m_fgCFG = (char*)memCalloc(strlen(pszFile) + 1);
		strcpy(m_fgCFG, pszFile);
	}

	return	TRUE;
}

/*---------------------------------------------------------
  FRAME GRABBER APPLICATION
---------------------------------------------------------*/
boolean SYSmgr::HFg_GrabSync(boolean bCont)
{
	m_pFg->GrabSync(bCont, NULL);

	return	TRUE;
}

/*
boolean SYSmgr::HFg_GrabSync1(dword camID)
{
	int32		w, h, ch;
	Img2D		o;
	
	m_pFg->SetCurrentDevice(camID);
	if(m_pFg->GetImageInfo(&w, &h, &ch) == TRUE)
	{
		o = IMP_ALLOC((byte)ch, w, h, TRUE);
		if(m_pFg->GrabSync(FALSE, (byte*)imptr(o)) == FALSE)
		{
			IMP_FREE(o);
			o = NULL;
			return FALSE;
		}
		else
		{
			Event_Pass(SYS_MGR, AREA_IMG_E, (dword)o, NULL);
			return TRUE;
		}
	}
	return FALSE;
}
*/

boolean SYSmgr::HFg_TakeShot(void)
{
	return TRUE;
}

boolean SYSmgr::HFg_GrabASync(boolean bCont)
{
	boolean		bDone = FALSE;

	if(bCont == FALSE)
	{
		LOGWARN("[HFg_GrabASync - FALSE] It has bug !");
	}

	bDone = m_pFg->GrabASync(bCont);
	
	return	bDone;
}

boolean SYSmgr::HFg_GrabStop(void)
{
	boolean		bDone = FALSE;

	bDone = m_pFg->GrabStop();

	return	bDone;
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
dword HFgDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser)
{
	SYSmgr		*pCxt = (SYSmgr*)pUser;

	if(pCxt)
	{
		return	pCxt->HEvent_Handler(HFG_DEV, dwEvent, wParam, lParam);
	}
	return	(0);
}

/*--- END OF SYSmgr_HFg.CPP ---*/
